package com.multi.thread.chapter04.demo08;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 *	getWaitQueueLength(Condition conditon)返回等待与此锁定相关的给定条件Condition的线程估计数
 */
public class TestWaitQueueLength {
	
	private ReentrantLock lock = new ReentrantLock();
	private Condition condition = lock.newCondition();
	
	public void waitMethod() {
		try {
			lock.lock();
			condition.await();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} finally {
			lock.unlock();
		}
	}
	
	public void notifyMethod() {
		try {
			lock.lock();
			System.out.println("有" + lock.getWaitQueueLength(condition) + "个线程正在等待Condition!");
			condition.signal();
		} finally {
			lock.unlock();
		}
	}
	
	public static void main(String[] args) throws InterruptedException {
		final TestWaitQueueLength service = new TestWaitQueueLength();
		Runnable run = new Runnable() {
			@Override
			public void run() {
				service.waitMethod();
			}
		};
		Thread[] thread = new Thread[10];
		for (int i = 0; i < 10; i++) {
			thread[i] = new Thread(run);
			thread[i].start();
		}
		Thread.sleep(200);
		service.notifyMethod();
		
	}

}
